<?php

function dd($var){
    echo '<pre>';
    print_r($var);
    exit;
}
$data = $_POST['date'];
$maybeMonth = key($data);
$date = $data[$maybeMonth];
$monStDate = date($maybeMonth.'-1');//本月一号
$monStTime = strtotime($monStDate);//本月一号对应时间戳
$weekMonSt = date('N',$monStTime);//本月一号星期几
$monDays = date('t',$monStTime);
$nightOvertime = 0;
$morningOvertime = 0;
$workStartTimeStr = "09:00:00";//公司上班时间
$workEndTimeStr = "18:30:00";//公司下班时间
$overTimeStr = "21:00:00";
$overTimes = 0;
$overTime2Str= "21:30:00";
$overTimes2= 0;
for($d=1;$d<=$monDays;$d++){
    if($date['st_hour'][$d]){
        $workStart = strtotime(date($maybeMonth.'-'.$d." {$workStartTimeStr}"));
        $trueWorkStart = strtotime(date($maybeMonth.'-'.$d." ".$date['st_hour'][$d].":".$date['st_min'][$d].":00"));
        $morningOvertime = $morningOvertime+($workStart-$trueWorkStart);
        $workEnd = strtotime(date($maybeMonth.'-'.$d." {$workEndTimeStr}"));
        if($date['nd_hour'][$d]<=12){
            $trueworkEnd= strtotime(date($maybeMonth.'-'.$d." ".$date['nd_hour'][$d].":".$date['nd_min'][$d].":00"));
            $trueworkEnd = $trueworkEnd+86400;
        }else{
            $trueworkEnd= strtotime(date($maybeMonth.'-'.$d." ".$date['nd_hour'][$d].":".$date['nd_min'][$d].":00"));
        }
        $nightOvertime = $nightOvertime+($trueworkEnd-$workEnd);
        $todayOverTime = strtotime(date($maybeMonth.'-'.$d." ".$overTimeStr));
        $todayOverTime2 = strtotime(date($maybeMonth.'-'.$d." ".$overTime2Str));
        if(($trueworkEnd-$todayOverTime)>=0){
            $overTimes++;
        }
        if(($trueworkEnd-$todayOverTime2)>=0){
            $overTimes2++;
        }
    }
}
$nightOvertimeHour = floor($nightOvertime/3600);
$morningOvertimeHour = floor($morningOvertime/3600);
$totalHour = floor(($nightOvertime+$morningOvertime)/3600);
echo '<script>alert("当前计算的月份是：'.$maybeMonth.'\n晚上加班时长为:'.$nightOvertimeHour.'\n早上加班时长为: '.$morningOvertimeHour.' \n合计: '.$totalHour.' \n加班到9点次数:'.$overTimes.' \n 加班到9点半次数:'.$overTimes2.'")</script>';